package com.yyoo.doc.html.style;

import com.lowagie.text.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zzy
 * 对齐样式转换器
 */
public class AlignConverter extends StyleValueConverter<Integer>{

    private static final Map<String, Integer> alignmentMap = new HashMap<>();


    /**
     * 支持的样式列表
     */
    private static final Map<String,Integer> SUPPORT_STYLES = new HashMap<>();

    static {
        // 支持的样式及其默认值
        SUPPORT_STYLES.put(StyleAttr.TEXT_ALIGN, Element.ALIGN_LEFT);
        SUPPORT_STYLES.put(StyleAttr.ALIGN, Element.ALIGN_LEFT);
        SUPPORT_STYLES.put(StyleAttr.VERTICAL_ALIGN, Element.ALIGN_MIDDLE);
        SUPPORT_STYLES.put(StyleAttr.HORIZONTAL_ALIGN, Element.ALIGN_CENTER);

        // 对齐样式集
        alignmentMap.put("left", Element.ALIGN_LEFT);
        alignmentMap.put("right", Element.ALIGN_RIGHT);
        alignmentMap.put("center", Element.ALIGN_CENTER);
        alignmentMap.put("justify", Element.ALIGN_JUSTIFIED);
        alignmentMap.put("justify-all", Element.ALIGN_JUSTIFIED_ALL);
        alignmentMap.put("top", Element.ALIGN_TOP);
        alignmentMap.put("bottom", Element.ALIGN_BOTTOM);
        alignmentMap.put("middle", Element.ALIGN_MIDDLE);
    }

    @Override
    public boolean support(String styleKey) {
        return SUPPORT_STYLES.containsKey(styleKey);
    }

    @Override
    protected Integer defaultValue(String styleKey) {
        return SUPPORT_STYLES.get(styleKey);
    }

    @Override
    protected Integer convert(String styleValue) {
        if(styleValue == null){
            return null;
        }
        // 如果 alignment 为 null 则说明不支持的对齐样式值，将返回 defaultValue 方法默认值
        return alignmentMap.get(styleValue.toLowerCase());
    }
}
